package com.smart.buss.ctrl;

import com.smart.buss.service.BussAccountLogService;
import com.smart.buss.service.BussSetService;
import com.smart.buss.service.BussTixianService;
import com.smart.buss.service.BussUserService;
import com.smart.buss.vo.BussAccountLog;
import com.smart.buss.vo.BussSet;
import com.smart.buss.vo.BussTixian;
import com.smart.buss.vo.BussUser;
import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.common.Constants;
import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.common.util.Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.WebUtils;

import java.util.List;

@RestController
@RequestMapping("/buss/tixian")
public class BussTixianController extends BaseCtrl {

	@Autowired
	private BussTixianService bussTixianService;

	@Autowired
	private BussUserService bussUserService;

	@Autowired
	private BussSetService bussSetService;
	@Autowired
	private BussAccountLogService bussAccountLogService;

	@RequestMapping
	public AjaxResult getList(BussTixian entity) throws Exception{
		List<BussTixian> list=bussTixianService.queryListByPage(entity);
		return getResult(entity, list);
	}

	@RequestMapping("list")
	public List<BussTixian> list(BussTixian entity) throws Exception{
		return bussTixianService.queryList(entity);
	}

	@RequestMapping("/save")
	@Transactional
	public AjaxResult save(BussTixian entity) throws Exception{
		String phone = getUserMobile();
		if(StringUtils.isNotBlank(phone)){
			entity.setUserId(phone);
		}
		if(StringUtils.isBlank(entity.getUserId()) || entity.getPirce()==null){
			return AjaxResult.failure("参数不足");
		}

		BussUser bu=new BussUser();
		bu.setId(entity.getUserId());
		BussUser query=bussUserService.queryObject(bu);

		if(query==null){
			return AjaxResult.failure("参数错误");
		}

		if(StringUtils.isBlank(entity.getId())){

			BussSet set=bussSetService.queryObject(null);
			if(set.getTixian()>entity.getPirce()){
				return AjaxResult.failure("提现未达到最低限额");
			}
			if(query.getTixianStatus()==1){
				return AjaxResult.failure("有正在提现的申请");
			}
			if(entity.getPirce()>query.getAccount()){
				return AjaxResult.failure("账户金额不足");
			}
			query.setTixianStatus(1);
			bussUserService.update(query);
			entity.setId(Util.getUUID());
			bussTixianService.insert(entity);
			return AjaxResult.updateSuccess();
		}else {
			if(entity.getTixianStatus()==1 || entity.getTixianStatus()==2){

				if(entity.getTixianStatus()==1){
					//提现成功
					query.setAccount(query.getAccount()-entity.getPirce());
					//添加账户记录
					BussAccountLog log=new BussAccountLog();
					log.setId(Util.getUUID());
					log.setUserId(entity.getUserId());
					log.setTaskId(entity.getId());
					log.setPrice(-entity.getPirce());
					log.setAddSub(0);
					log.setLogtype(-1);
					log.setRemarks("提现");
					bussAccountLogService.insert(log);
				}
				query.setTixianStatus(0);
				bussUserService.update(query);
				bussTixianService.update(entity);
			}
			return AjaxResult.saveSuccess();
		}
	}

	@RequestMapping("/del")
	public AjaxResult del(String id) throws Exception{
		bussTixianService.delete(id);
		return AjaxResult.deleteSuccess();
	}
}
